package Dao;

import DataClass.LoginAndExit;
import Util.JBDCUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginAndExitDao {

    private static PreparedStatement getIsStudentStatement = null;

    private static PreparedStatement checkCorrect = null;

    private static PreparedStatement getUserTypeStatement = null;

    private static PreparedStatement insertLoginAndExitDataStatement = null;
    /**
     * 判断是否是学生
     */
    public static boolean isStudentDataWithAccount(String Account) throws SQLException {
        if(getIsStudentStatement == null){
            String sql = "select * from student where Account=?";
            getIsStudentStatement = JBDCUtil.getPreparedStatement(sql);
        }
        boolean isStudent = false;
        getIsStudentStatement.setString(1, Account);
        ResultSet resultSet = getIsStudentStatement.executeQuery();
        JBDCUtil.commit();
        if (resultSet.next()) {
            int count = resultSet.getInt("count");
            isStudent = count > 0;
        }
        return isStudent;
    }

    /**
     * 判断账号密码是否正确
     */
    public static boolean isCorrect(String Account, String Password) throws SQLException {
        if(checkCorrect == null)
        {
            String query = "SELECT * FROM Registered_users WHERE RU_no = ? AND RU_password = ?";
            checkCorrect = JBDCUtil.getPreparedStatement(query);
        }

        checkCorrect.setString(1, Account);
        checkCorrect.setString(2, Password);

        ResultSet resultSet = checkCorrect.executeQuery();
        JBDCUtil.commit();
        return resultSet.next();
    }

    /**
     * 获取某一个账户的类型
     * @param Account 账户id
     * @return 类型ResultSet
     * @throws SQLException 数据库异常
     */
    public static ResultSet getUserType(String Account) throws SQLException {
        if(getUserTypeStatement == null){
            String sql = "SELECT ru_type FROM Registered_users WHERE RU_no = ?";
            getUserTypeStatement = JBDCUtil.getPreparedStatement(sql);
        }
        getUserTypeStatement.setString(1, Account);
        ResultSet resultSet = getUserTypeStatement.executeQuery();
        JBDCUtil.commit();
        return resultSet;
    }

    public static Boolean insertLoginAndExitData(LoginAndExit loginAndExit) throws SQLException {
        if(insertLoginAndExitDataStatement == null)
        {
            String sql = "INSERT INTO Login_And_Exit (RU_ID, Info_Type, Info_InTime, Info_OutTime,RU_Teacher, RU_AnoTeacher,rp_name) VALUES (?, ?, ?, ?, ?,?,?)";
            insertLoginAndExitDataStatement = JBDCUtil.getPreparedStatement(sql);
        }

        insertLoginAndExitDataStatement.setString(1,loginAndExit.getRUid());
        insertLoginAndExitDataStatement.setString(2,loginAndExit.getInfoType());
        insertLoginAndExitDataStatement.setTimestamp(3,loginAndExit.getInfoInTime());
        insertLoginAndExitDataStatement.setTimestamp(4,loginAndExit.getInfoOutTime());
        insertLoginAndExitDataStatement.setString(5,loginAndExit.getRUTeacher());
        insertLoginAndExitDataStatement.setString(6,loginAndExit.getRUAnoTeacher());
        insertLoginAndExitDataStatement.setString(7,loginAndExit.getRPName());

        Boolean result = insertLoginAndExitDataStatement.executeUpdate() > 0;
        JBDCUtil.commit();
        return result;
    }

    /**
     * 获取某一个账户是否为学生
     * @param account 账户id
     * @return 一个布尔值
     * @throws SQLException 数据库异常
     */
    public static Boolean isStudent(String account) throws SQLException {
        ResultSet rs = null;
        rs = LoginAndExitDao.getUserType(account);
        while(rs.next()){
            if(rs.getString("ru_type").equals("ST")){
                JBDCUtil.closeResultSet(rs);
                return true;
            }
        }
        JBDCUtil.closeResultSet(rs);
        return false;
    }
}
